import { msmReceiveMap, msmRecordMap } from '@/config/dict'
import { findDict } from '@/utils/index'

export const queryFormItem = [
  {
    type: 'date',
    key: 'beginDate',
    // label: '开始时间',
    otherAttrs: {
      style: {
        width: '140px'
      },
      placeholder: '开始日期'
    }
  },
  {
    type: 'date',
    key: 'endDate',
    // label: '结束时间',
    otherAttrs: {
      style: {
        width: '140px'
      },
      placeholder: '结束日期'
    }
  },
  {
    type: 'select',
    key: 'warningLevel',
    // label: ' 预警级别',
    options: msmRecordMap,
    otherAttrs: {
      style: {
        width: '110px'
      },
      placeholder: '预警级别'
    }
  },
  {
    type: 'select',
    key: 'receiver',
    // label: '接收人',
    options: msmReceiveMap,
    otherAttrs: {
      style: {
        width: '110px'
      },
      placeholder: '接收人'
    }
  },
  {
    type: 'input',
    key: 'keyword',
    // label: '请输入业务名称或标题关键字',
    otherAttrs: {
      style: {
        width: '200px'
      },
      placeholder: '请输入业务名称或标题关键字'
    }
  }
]

// 预警状态格式化
const format = records => {
  return findDict(msmRecordMap, records.row.warningLevel)
}
// 处理人数过多
const filterNumPeople = value => {
  var exportValue = ''
  const result = value.split(',')
  if (result.length > 5) {
    exportValue = `（共${result.length}人）${result.slice(0, 5).join('、')}...`
  } else {
    exportValue = result.join('、')
  }
  return exportValue
}

export const tableColumn = (page, size, handleReceiver) => {
  return [
    // {
    //   key: 'businessName',
    //   label: '业务名称',
    //   minWidth: 120
    // },
    {
      key: 'warningLevel',
      label: '预警级别',
      minWidth: 80,
      render: (h, scope) => (
        <span>
          <span
            style={
              'width:6px; height:6px; display: inline-block; border-radius: 50%;margin-right: 8px; background-color:' +
              format(scope).color
            }
          />
          <span>{scope.row.warningName}</span>
        </span>
      )
    },
    {
      key: 'content',
      label: '短信内容',
      minWidth: 300
    },
    {
      key: 'receiverDesc',
      label: '接收人',
      minWidth: 170,
      render: (h, scope) => (
        <el-link onClick={() => handleReceiver(scope.row)}>
          {filterNumPeople(scope.row.receiverDesc)}
        </el-link>
      )
    },
    {
      key: 'createTime',
      label: '发送时间',
      minWidth: 120
    }
  ]
}

// 处理状态的文字和状态显示
const formatStatusName = scope => {
  let name = ''
  let color = ''
  switch (scope.row.status) {
    case 1:
      name = '成功'
      break
    case 2:
      name = '失败'
      color = '#f00'
      break
    case 3:
      name = '未发送'
      break
    default:
      break
  }
  return { name, color }
}
// 接收人弹窗
export const receiverTableColumn = (page, size) => {
  return [
    // {
    //   key: 'N0',
    //   label: 'No',
    //   minWidth: 120
    // },
    {
      key: 'deptName',
      label: '部门',
      minWidth: 120
    },
    {
      key: 'accountName',
      label: '姓名',
      minWidth: 120
    },
    {
      key: 'status',
      label: '状态',
      minWidth: 120,
      render: (h, scope) => (
        <span style={'color: ' + formatStatusName(scope).color}>
          {formatStatusName(scope).name}
        </span>
      )
    },
    {
      key: 'failReason',
      label: '失败原因',
      minWidth: 200
    }
  ]
}
